﻿@model DatabaseEntities
@{
    string userToken = ViewBag.UserToken;
    string fullName = ViewBag.FullName;
    var user = userToken.GetAccountByUserToken(Model);

    ViewBag.Title = user.FullName + " | DTweet";

    var currentUser = Static.CurrentUser;
    var currentUserToken = "NaN";
    var anyoneOnline = currentUser != null;
    
    var tweets = Model.GetTweetsForUserPage(user.UniqueToken);
    var circles = Model.Circles.AsQueryable();
    if(anyoneOnline)
    {
        currentUserToken = currentUser.UniqueToken;
        circles = Model.Circles.Where(c => c.UserToken == currentUser.UniqueToken);
    }

    var h1 = user.FullName.Truncate(25, false) + " on DTweet";

    var userInfo = new UserInfo(Model, user.UniqueToken);

    var following = Model.GetUsersFromHisCircles(user.UniqueToken);
    var followers = Model.GetOtherUsersWhoCircledHim(user.UniqueToken);
}
@section Meta
{
    <link rel="alternate" type="application/rss+xml" title="@user.FullName - Upcoming Tweets" href="@Url.Content("~/RSS/" + user.UniqueToken)" />    
}
@section H1 {
    @h1
}
@section UserName
{    
    @if (anyoneOnline && currentUserToken != user.UniqueToken)
    {
        <div id="you" accesskey="@currentUserToken">@currentUser.FullName.Truncate(20)</div>
    }
    <div id="username">@user.FullName.Truncate(20)</div>
}
@section AboutUser
{
    <div id="about-user" class="about-user">
        @if (anyoneOnline && user.UniqueToken != currentUserToken)
        {
            var inAnyCircle = Model.Circles.Where(c => c.UserToken == currentUserToken && c.MembersCount > 0 && c.Members.Contains(user.UniqueToken)).FirstOrDefault() != null;
            if (inAnyCircle)
            {
                <div id="follow-user" class="green-button">
                    <img src="@Url.Content("~/Content/Images/choose-dim.png")" style="vertical-align: text-bottom;" width="15" height="15" />
                    <span style="color:White;">In your circles</span>
                </div>
            }
            else
            {
                <div id="follow-user" class="red-button">
                    <img src="@Url.Content("~/Content/Images/choose-dim.png")" style="vertical-align: text-bottom;" width="15" height="15" />
                    <span style="color:White;">Not in your circles</span>
                </div>            
            }
            
            <div id="circles-context-menu" class="circles-context-menu">
                <div id="circles-list">
                    @if (circles.Count() > 0)
                    {
                        foreach (var circle in circles)
                        {
                            if (circle.MembersCount > 0 && circle.Members.Contains(user.UniqueToken))
                            {
                        <div class="circle-name in-circle">
                            <div style="float: right;color:White;" class="circleMembersCount">@circle.MembersCount</div>
                            <div class="circleName" style="color:White;">@circle.Name</div>
                        </div>
                        <input type="hidden" id="in-circle" value="true" />
                            }
                            else
                            {
                        <div class="circle-name">
                             <div style="float: right;" class="circleMembersCount">@circle.MembersCount</div>
                            <div class="circleName">@circle.Name</div>
                        </div>
                        <input type="hidden" id="in-circle" value="false" />
                            }
                        }
                    }
                    else
                    {
                        <p id="no-circle" style="padding: 5px 10px; color: Red;">
                            No circle yet!
                        </p>
                    }
                </div>
                <p id="create-circle-p" style="display: none; padding: 5px 10px;">
                    <input id="circle-name-text" type="text" style="width:111px;" />
                    <span id="create-circle-now" class="blue-button">Create</span>
                </p>
                <div id="create-circle" style="border-top: 1px solid #D7D7D7;">
                    + Create new</div>
            </div>

            <input type="hidden" id="user-token" value="@user.UniqueToken" />
        }
        
        @if (anyoneOnline && user.UniqueToken == currentUser.UniqueToken)
        { <h2 class="user-name">@user.FullName <span style="color:Gray;">(You!)</span></h2>}
        else
        {<h2 class="user-name">@user.FullName</h2>}
        @{var isInCircles = Model.Circles.Where(c => c.UserToken == user.UniqueToken && c.MembersCount > 0 && c.Members.Contains(currentUserToken)).FirstOrDefault() != null;}
        @if (isInCircles)
        {
            <p style="padding-left: 6px;color: green;">You are in <strong style="color: green;">@user.FullName</strong>'s circles</p>
        }
        
        <table class="user-info-table">
            @if (user.About != null)
            {
                <tr>
                    <td>
                        <h2>
                            About</h2>
                    </td>
                    <td>
                        @Html.Raw(user.About.ReplaceAll())
                    </td>
                </tr>
            }
            @if (user.Website != null)
            {
                <tr>
                    <td>
                        <h2>
                            Website</h2>
                    </td>
                    <td>
                        <a href="@user.Website">@user.Website.Replace("http://", "")</a>
                    </td>
                </tr>
            }
            <tr>
                <td>
                    <h2>
                        Posts count</h2>
                </td>
                <td>
                    @userInfo.PostsCount public tweets
                    @if (anyoneOnline && user.UniqueToken == Static.CurrentUser.UniqueToken)
                    {
                        <span>- and - @userInfo.PrivatePostsCount private tweets (Total: @(userInfo.PostsCount + userInfo.PrivatePostsCount) tweets)</span>
                    }
                </td>
            </tr>
            <tr>
                <td>
                    <h2>
                        Following</h2>
                </td>
                <td>
                    @following.Count users <span style="color:Gray;">(@following.Members)</span>
                </td>
            </tr>
            <tr>
                <td>
                    <h2>
                        Followers</h2>
                </td>
                <td>
                    @followers.Count users <span style="color:Gray;">(@followers.Members)</span>
                </td>
            </tr>
        </table>
    </div>
}
@if (anyoneOnline && user.UniqueToken == currentUser.UniqueToken)
{
    <div class="user-image">
        <img id="gravatar-image" src="@currentUser.Gravatar" itemprop="image" width="60" height="60" /></div>
    <div class="tweet-section">
        <div>
            <textarea id="tweetMessage"></textarea>
            <div style="height: 24px;">
                <div id="tweet-button">
                    Share</div>
                <div id="share-to-result" class="share-to-result">
                </div>
                <div id="share-to" class="share-to">
                    @foreach (var c in circles)
                    {
                        <div class="circle-name" accesskey="@c.Name" id="@c.Token">
                            @c.Name <span>(@c.MembersCount)</span>
                        </div>
                    }
                    <div accesskey="Public" id="Public">
                        Public</div>
                </div>
                <div id="add-more" class="add-more">
                    + Add people to share</div>
            </div>
        </div>
    </div>
}
@if (tweets.Count() <= 0 && (!anyoneOnline || currentUser.UniqueToken != user.UniqueToken))
{
    <div style="padding: 20px; font-size: 20px; border: 1px solid #a10a34; color:#a10a34">@user.FullName seems very busy!!!! --- No post post shared publicly!!</div>
}
<div id="tweets-container">
    @foreach (var tweet in tweets)
    {
        var tweetBy = tweet.UserToken.GetAccountByUserToken(Model);
        <div class="each-tweet-container">
            @* << This DIV is needed at DELETE action; for removing whole post: including user-image *@
            <div class="user-image">
                <img src="@tweetBy.Gravatar" itemprop="image" width="60" height="60" alt="" /></div>
            <div class="tweet-section" id="@tweet.TweetToken">
                <div class="manage-tweet">
                </div>
                @if (tweet.UserToken == currentUserToken)
                { <input id="owner" type="hidden" value="owner" /> }
                else
                { <input id="owner" type="hidden" value="not-owner" /> }
                <div id="post-options" class="post-options">
                </div>
                <h2 class="name-section">
                    <a href="@Url.Content("~/" + tweet.UserToken)">@tweetBy.FullName</a>
                    -
                    @if (tweet.EditingTime != null)
                    {
                        <a href="@Url.Content("~/" + tweet.TweetToken + "/" + tweet.Message.FetchPostUrl())" class="p-Link">
                            <span class="time"><time class="time" itemprop="date" datetime="@tweet.TweetingTime.ToMachineTime()" pubdate>@tweet.TweetingTime</time></span>
                            <span class="edit-time">
                                <abbr title="Last edit time: @tweet.EditingTime">
                                    (@tweet.EditCount.GetEditTimes())</abbr>
                            </span></a>
                    }
                    else
                    {
                        <a href="@Url.Content("~/" + tweet.TweetToken + "/" + tweet.Message.FetchPostUrl())" class="p-Link">
                            <span class="time"><time class="time" itemprop="date" datetime="@tweet.TweetingTime.ToMachineTime()" pubdate>@tweet.TweetingTime</time></span>
                        </a>
                    }
                </h2>
                <div class="tweet-message">
                    @Html.Raw(tweet.Message.HandleAllThings())
                </div>
            </div>
        </div>
    }
</div>
@section JavaScript
{
    <script src="@Url.Content("~/Content/JavaScript/JsDecoder.js")" type="text/javascript"> </script>
    <script src="@Url.Content("~/Content/JavaScript/dashingquill.user.js")" type="text/javascript"> </script>
    <script>
        user.handleEvents();
</script>
}